const sql = require("../../mysql");

module.exports = async ctx => {
  try {
    const { parentId } = ctx.request.body;

    parentIdOption = parentId || { [sql.Op.is]: null };

    let fileList = await sql.Vfile.findAll({
      attributes: [
        sql.Sequelize.col("rfile.size"),
        sql.Sequelize.col("rfile.type"),
        sql.Sequelize.col("rfile.path"),
        "name",
        "id",
        "isFolder",
        "createdAt",
        "updatedAt",
        "parentId"
      ],
      include: [
        {
          model: sql.Rfile,
          as: "rfile",
          attributes: []
        }
      ],
      where: {
        userId: ctx.request.userId,
        parentId: parentIdOption
      },
      raw: true
    });

    ctx.body = {
      code: 0,
      msg: "成功",
      data: fileList
    };
  } catch (error) {
    console.log(error);
    ctx.response.status = 200;

    ctx.body = {
      code: -1,
      msg: error.errors ? error : error.errors[0].message
    };
  }
};
